home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / oper_sys / mach / amiga / newsim.lha / extract.c next >
Encoding:
C/C++ Source or Header  |  1993-03-20  |  1.9 KB  |  123 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define HUNK_CODE 1001
  5.  
  6. typedef int int32;
  7.  
  8. inline int32
  9. ntohl (char *n)
  10. {
  11.   return *(int32 *)n;
  12. }
  13.  
  14. inline int
  15. read_long (FILE *fd, int32 *i)
  16. {
  17.   char buf[4];
  18.  
  19.   fread (buf, 4, 1, fd);
  20.   if (feof (fd))
  21.     return 0;
  22.   if (ferror (fd))
  23.     {
  24.       perror ("fread");
  25.       return -1;
  26.     }
  27.   *i = ntohl (buf);
  28.   return 1;
  29. }
  30.  
  31. int
  32. main (int argc, char **argv)
  33. {
  34.   int32 type;
  35.   FILE *in;
  36.   int diag;
  37.  
  38.   if (argc != 3)
  39.     {
  40.       fprintf (stderr, "usage: extract infile outfile\n");
  41.       exit (8);
  42.     }
  43.   in = fopen (argv[1], "r");
  44.   if (!in)
  45.     {
  46.       perror (argv[1]);
  47.       exit (9);
  48.     }
  49.   while ((diag = read_long (in, &type)) == 1)
  50.     {
  51.       int32 size;
  52.  
  53.       fprintf (stderr, "Hunk type %d\n", type);
  54.  
  55.       switch (read_long (in, &size))
  56.     {
  57.     case 0:
  58.       fprintf (stderr, "Unexpected end of file\n");
  59.       fclose (in);
  60.       exit (2);
  61.       break;
  62.     case -1:
  63.       fclose (in);
  64.       exit (11);
  65.     }
  66.       size *= 4;
  67.       if (type == HUNK_CODE)
  68.     {
  69.       void *code = malloc (size);
  70.       FILE *out;
  71.       if (!code)
  72.         {
  73.           fprintf (stderr, "Out of memory\n");
  74.           fclose (in);
  75.           exit (7);
  76.         }
  77.       fread (code, size, 1, in);
  78.       if (feof (in))
  79.         {
  80.           fprintf (stderr, "Unexpected end of file\n");
  81.           free (code);
  82.           fclose (in);
  83.           exit (4);
  84.         }
  85.       if (ferror (in))
  86.         {
  87.           perror ("fread");
  88.           free (code);
  89.           fclose (in);
  90.           exit (5);
  91.         }
  92.       fclose (in);
  93.       out = fopen (argv[2], "w");
  94.       if (!out)
  95.         {
  96.           perror (argv[2]);
  97.           exit (10);
  98.         }
  99.       fwrite (code, size, 1, out);
  100.       free (code);
  101.       if (ferror (out))
  102.         {
  103.           perror ("fwrite");
  104.           fclose (out);
  105.           exit (6);
  106.         }
  107.       fclose (out);
  108.       exit (0);
  109.     }
  110.       else
  111.     {
  112.       if (fseek (in, size, 1))
  113.         {
  114.           perror ("fseek");
  115.           fclose (in);
  116.           exit (3);
  117.         }
  118.     }
  119.     }
  120.   fclose (in);
  121.   return diag == -1 ? 12 : 0;
  122. }
  123.